package com.neu.service;

import org.apache.ibatis.annotations.Param;
import com.neu.dao.GoodsProductMapper;
import com.neu.dao.ShoppingGoodsProductMapper;
import com.neu.domain.ShoppingGoodsProduct;
import com.neu.domain.ShoppingGoodsProductExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingGoodsProductService {
    @Resource
    private ShoppingGoodsProductMapper ShoppingGoodsProductMapper;
    @Resource
    private GoodsProductMapper goodsProductMapper;

    public List<ShoppingGoodsProduct> queryByGid(Integer gid) {
        ShoppingGoodsProductExample example = new ShoppingGoodsProductExample();
        example.or().andGoodsIdEqualTo(gid).andDeletedEqualTo(false);
        return ShoppingGoodsProductMapper.selectByExample(example);
    }

    public ShoppingGoodsProduct findById(Integer id) {
        return ShoppingGoodsProductMapper.selectByPrimaryKey(id);
    }

    public void deleteById(Integer id) {
        ShoppingGoodsProductMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(ShoppingGoodsProduct goodsProduct) {
        goodsProduct.setAddTime(LocalDateTime.now());
        goodsProduct.setUpdateTime(LocalDateTime.now());
        ShoppingGoodsProductMapper.insertSelective(goodsProduct);
    }

    public int count() {
        ShoppingGoodsProductExample example = new ShoppingGoodsProductExample();
        example.or().andDeletedEqualTo(false);
        return (int) ShoppingGoodsProductMapper.countByExample(example);
    }

    public void deleteByGid(Integer gid) {
        ShoppingGoodsProductExample example = new ShoppingGoodsProductExample();
        example.or().andGoodsIdEqualTo(gid);
        ShoppingGoodsProductMapper.logicalDeleteByExample(example);
    }

    public int addStock(Integer id, Short num){
        return goodsProductMapper.addStock(id, num);
    }

    public int reduceStock(Integer id, Short num){
        return goodsProductMapper.reduceStock(id, num);
    }
}